package com.gmail.hh.flickrj.sample.android.fragments;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;

import com.gmail.hh.flickrj.sample.android.R;


@SuppressLint("NewApi")
public class FragmentGallery2 extends Fragment implements
OnItemSelectedListener, ViewFactory {

	private static final Logger logger = LoggerFactory.getLogger(FragmentGallery2.class);

	public static final String TAG = "fragment_gallery2";
	private Activity mActivity = null;

	@Override
	public void onAttach(Activity activity) {
	    super.onAttach(activity);
	    mActivity=activity;
	} 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    // TODO Auto-generated method stub
	    super.onCreate(savedInstanceState);
	}

	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup vg, Bundle data) {
		// Inflate the  layout file.
		View view = inflater.inflate(R.layout.fragment_gallery2, null);
	
	    mSwitcher = (ImageSwitcher) view.findViewById(R.id.switcher2);
	    mSwitcher.setFactory(this);
	    mSwitcher.setInAnimation(AnimationUtils.loadAnimation(mActivity,
	            android.R.anim.fade_in));
	    mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(mActivity,
	            android.R.anim.fade_out));
	
	    Gallery g = (Gallery) view.findViewById(R.id.gallery2);
	    g.setAdapter(new ImageAdapter(mActivity));
	    g.setOnItemSelectedListener(this);
	    
	    return view;
	}
	
	public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
	    mSwitcher.setImageResource(mImageIds[position]);
	}
	
	public void onNothingSelected(AdapterView<?> parent) {
	}
	
	public View makeView() {
	    ImageView i = new ImageView(mActivity);
	    i.setBackgroundColor(0xFF000000);
	    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
	    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
	            LayoutParams.MATCH_PARENT));
	    return i;
	}

	private ImageSwitcher mSwitcher;

	public class ImageAdapter extends BaseAdapter {
	    public ImageAdapter(Context c) {
	        mContext = c;
	    }
	
	    public int getCount() {
	        return mThumbIds.length;
	    }
	
	    public Object getItem(int position) {
	        return position;
	    }
	
	    public long getItemId(int position) {
	        return position;
	    }
	
	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView i = new ImageView(mContext);
	
	        i.setImageResource(mThumbIds[position]);
	        i.setAdjustViewBounds(true);
	        i.setLayoutParams(new Gallery.LayoutParams(
	                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	 //       i.setBackgroundResource(R.drawable.picture_frame);
	        return i;
	    }

    private Context mContext;

	}
	
	private Integer[] mThumbIds = {
	        R.drawable.ic_action_list, R.drawable.ic_action_mail, R.drawable.ic_action_new,
	        R.drawable.ic_action_new};
	
	private Integer[] mImageIds = {
	        R.drawable.sample_thumb_0, R.drawable.sample_thumb_1, R.drawable.sample_thumb_2,
	        R.drawable.sample_thumb_3};

}


	

	


